package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.ILoginMapper;
import com.cuixichang.minimalism.transfer.dto.ILoginDTO;
import com.cuixichang.minimalism.transfer.factory.ILoginAssembler;
import com.cuixichang.minimalism.transfer.po.ILogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 系統登录账户管理表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-22
 */
@Service
public class ILoginServiceImpl implements ILoginService {

    @Autowired
    private ILoginMapper iLoginMapper;

    @Override
    public ILoginDTO selectById(Long id){
        ILogin iLogin = iLoginMapper.selectById(id);
        return ILoginAssembler.INSTANCT.convertDto(iLogin);
    }

    @Override
    public ILoginDTO selectOne(ILoginDTO iLoginDTO){
        Assert.notNull(iLoginDTO,"参数为空,请检查");
        ILogin iLogin = ILoginAssembler.INSTANCT.convertPo(iLoginDTO);
        ILogin result = iLoginMapper.selectOne(new QueryWrapper<>(iLogin));
        return ILoginAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<ILoginDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<ILogin> results = iLoginMapper.selectBatchIds(ids);
        return results.stream().map(ILoginAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<ILoginDTO> selectList(ILoginDTO iLoginDTO){
        return this.selectList(iLoginDTO, (iLogin, queryWrapper) -> {});
    }

    @Override
    public List<ILoginDTO> selectList(ILoginDTO iLoginDTO, BiConsumer<ILogin, QueryWrapper<ILogin>> consumer){
        Assert.notNull(iLoginDTO,"参数为空,请检查");
        ILogin iLogin = ILoginAssembler.INSTANCT.convertPo(iLoginDTO);
        QueryWrapper<ILogin> queryWrapper = new QueryWrapper<>(iLogin);
        if(Objects.nonNull(consumer)){
            consumer.accept(iLogin, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<ILogin> results = iLoginMapper.selectList(queryWrapper);
        return results.stream().map(ILoginAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<ILoginDTO> selectPage(ILoginDTO iLoginDTO,long pageSize,long pageNo){
        return this.selectPage(iLoginDTO, pageSize, pageNo, (iLogin, queryWrapper) -> {});
    }

    @Override
    public Page<ILoginDTO> selectPage(ILoginDTO iLoginDTO,long pageSize,long pageNo, BiConsumer<ILogin, QueryWrapper<ILogin>> consumer){
        Assert.notNull(iLoginDTO,"参数为空,请检查");
        ILogin iLogin = ILoginAssembler.INSTANCT.convertPo(iLoginDTO);
        QueryWrapper<ILogin> queryWrapper = new QueryWrapper<>(iLogin);
        if(Objects.nonNull(consumer)) consumer.accept(iLogin, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<ILogin> page = iLoginMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<ILoginDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(ILoginAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iLoginMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iLoginMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(ILoginDTO iLoginDTO){
        return deleteByCondition(iLoginDTO, (iLogin, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(ILoginDTO iLoginDTO, BiConsumer<ILogin, QueryWrapper<ILogin>> consumer){
        Assert.notNull(iLoginDTO,"参数为空,请检查");
        ILogin iLogin = ILoginAssembler.INSTANCT.convertPo(iLoginDTO);
        QueryWrapper<ILogin> queryWrapper = new QueryWrapper<>(iLogin);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iLogin, queryWrapper);
        }
        return iLoginMapper.delete(queryWrapper);
    }

    @Override
    public ILoginDTO insert(ILoginDTO iLoginDTO){
        return insertHasOption(iLoginDTO, (ILoginDTO) -> null);
    }

    @Override
    public ILoginDTO insertHasOption(ILoginDTO iLoginDTO, Function<ILoginDTO,ILoginDTO> preOption){
        Assert.notNull(iLoginDTO,"参数为空,请检查");
        iLoginDTO.check();
        ILogin iLogin = ILoginAssembler.INSTANCT.convertPo(iLoginDTO);
        Long createLoginId = iLogin.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iLogin.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iLogin.setCreateTime(localDateTime);
        iLogin.setUpdateTime(localDateTime);
        Boolean isEnable = iLogin.getIsEnable();
        if(Objects.isNull(isEnable)) iLogin.setIsEnable(true);
        Integer sort = iLogin.getSort();
        if(Objects.isNull(sort)) iLogin.setSort(0);
        if(Objects.nonNull(preOption)){
            ILoginDTO apply = preOption.apply(iLoginDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iLoginMapper.insert(iLogin);
        Assert.isTrue(insert>0,"插入失败");
        return ILoginAssembler.INSTANCT.convertDto(iLogin);
    }

    @Override
    public int updateById(ILoginDTO iLoginDTO){
         return updateById(iLoginDTO, (iLogin, updateWrapper) -> {});
    }

    @Override
    public int updateById(ILoginDTO iLoginDTO, BiConsumer<ILogin, UpdateWrapper<ILogin>> consumer){
        Assert.notNull(iLoginDTO,"参数为空,请检查");
        Long id = iLoginDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        ILogin iLogin = ILoginAssembler.INSTANCT.convertPo(iLoginDTO);
        Long createLoginId = iLogin.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iLogin.setCreateLoginId(null);
        iLogin.setUpdateLoginId(createLoginId);
        iLogin.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<ILogin> updateWrapper = new UpdateWrapper<ILogin>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iLogin,updateWrapper);
        return iLoginMapper.update(iLogin,updateWrapper);
    }
}
